package com.sinodata.bsm.center.bean;

import java.util.ArrayList;
import java.util.List;

import com.sinodata.bsm.center.cache.ProcessEventTypeRuleCache;
import com.sinodata.bsm.center.cache.ProcessExcludeResRuleCache;
import com.sinodata.bsm.center.cache.ProcessRuleCache;
import com.sinodata.bsm.center.util.spring.SpringContextHolder;
import com.sinodata.bsm.common.vo.ProcessEventTypeRule;
import com.sinodata.bsm.common.vo.ProcessExcludeResRule;
import com.sinodata.bsm.common.vo.ProcessRule;

/**
 * <p>
 * Description: 发送工单规则的Bean对象
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-4-15 下午4:44:58          liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class ProcessRuleBean {

    /**
     * 工单规则缓存
     */
    private static ProcessRuleCache processRuleCache = SpringContextHolder.getBean(ProcessRuleCache.class);
    /**
     *工单事件类别缓存
     */
    private static ProcessEventTypeRuleCache processEventTypeRuleCache = SpringContextHolder.getBean(ProcessEventTypeRuleCache.class);
    /**
     * 工单排除子资源缓存
     */
    private static ProcessExcludeResRuleCache processExcludeResRuleCache = SpringContextHolder.getBean(ProcessExcludeResRuleCache.class);

    /**
     * 工单规则
     */
    private ProcessRule processRule;
    /**
     * 工单适应的事件类别规则
     */
    private List<ProcessEventTypeRule> eventTypeList;
    /**
     * 工单排除子资源规则
     */
    private List<ProcessExcludeResRule> excludeResList;

    public ProcessRuleBean() {

    }

    public ProcessRuleBean(ProcessRule processRule, List<ProcessEventTypeRule> eventTypeList, List<ProcessExcludeResRule> excludeResList) {
        this.processRule = processRule;
        this.eventTypeList = eventTypeList;
        this.excludeResList = excludeResList;
    }

    /**
     * 获取工单规则
     * @return
     */
    public ProcessRule getProcessRule() {
        return processRule;
    }

    /**
     * 获取工单事件规则
     * @return
     */
    public List<ProcessEventTypeRule> getEventTypeList() {
        return eventTypeList;
    }

    /**
     * 获取排除子资源
     * @return
     */
    public List<ProcessExcludeResRule> getExcludeResList() {
        return excludeResList;
    }

    /**
     * 获取通知规则Bean对象，数据从缓存中获取
     * 
     * @param id
     * @return
     */
    public static ProcessRuleBean get(Long id) {
        ProcessRule processRule = processRuleCache.get(id);
        List<ProcessEventTypeRule> eventTypeList = processEventTypeRuleCache.getByProcessRuleId(id);
        List<ProcessExcludeResRule> excludeResList = processExcludeResRuleCache.getByProcessRuleId(id);
        return new ProcessRuleBean(processRule, eventTypeList, excludeResList);
    }

    /**
     *  根据资源、事件类别、事件级别获取有效的通知规则
     * @param resId
     * @param eventTypeId
     * @param level
     * @return
     */
    public static List<ProcessRuleBean> getAvailableProcessRules(Long resId, Long eventTypeId, Integer level) {
        List<ProcessRuleBean> list = new ArrayList<ProcessRuleBean>();
        List<ProcessRule> rules = processRuleCache.getAllByRes(resId);
        //按级别来放入到 list 中
        for (ProcessRule processRule : rules) {
            String levels = processRule.getEventLevels();
            if (levels != null && levels.contains(level + "")) {
                list.add(ProcessRuleBean.get(processRule.getId()));
            }
        }

        //按是否禁用、排除资源、事件类别来，不符合的排除
        for (int m = list.size() - 1; m >= 0; m--) {
            ProcessRuleBean notifyRuleBean = list.get(m);
            //禁用的排除
            if (notifyRuleBean.processRule.getStatus() == 0) {
                list.remove(m);
                continue;
            }
            //排除排除资源的规则
            List<ProcessExcludeResRule> notifyExcludeResRules = notifyRuleBean.getExcludeResList();
            if (notifyExcludeResRules != null && notifyExcludeResRules.size() != 0) {
                for (ProcessExcludeResRule notifyExcludeResRule : notifyExcludeResRules) {
                    if (resId.equals(notifyExcludeResRule.getResId())) {
                        list.remove(m);
                        continue;
                    }
                }
            }

            //按事件类别移除
            List<ProcessEventTypeRule> notifyEventTypeRules = notifyRuleBean.getEventTypeList();
            if (notifyEventTypeRules != null && notifyEventTypeRules.size() != 0) {
                boolean remove = true;
                for (ProcessEventTypeRule notifyEventTypeRule : notifyEventTypeRules) {
                    if (notifyEventTypeRule.getEventTypeId().equals(eventTypeId)) {
                        remove = false;
                    }
                }
                if (remove) {
                    list.remove(m);
                }
            }
        }
        return list;
    }
}
